package org.viva.action.cms;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.viva.api.ActionUtils;
import org.viva.cms.sitemap.BaiduSm;
import org.viva.core.api.Ajax;
import org.viva.core.util.DeviceUtils;
import org.viva.core.util.UTIL;
import org.viva.entity.cms.Content;
import org.viva.secu.Security;
import org.viva.service.cms.CategoryService;
import org.viva.service.cms.ContentService;
import org.viva.service.cms.TagService;
import org.viva.service.sys.WebsiteCache;
import org.viva.service.wx.mp.AccessTokenService;

@Controller
public class ContentAction {

    @Resource
    private ContentService     contentService;
    @Resource
    private CategoryService    categoryService;
    @Resource
    private AccessTokenService accessTokenService;
    @Resource
    private TagService         tagService;

    @RequestMapping(value = "/a/{id}", method = RequestMethod.GET)
    public String page(HttpServletRequest request, @PathVariable("id") Long id, ModelMap modelMap) {
        modelMap.put("ws", WebsiteCache.getConfig());
        String cms_temppath = WebsiteCache.get("cms_temppath");
        Content content = contentService.get(id);
        modelMap.put("data", content);
        String temppath = content.getTemplate();
        if (DeviceUtils.isMobileDevice(request) && !"pc".equals(request.getParameter("_t"))) {
            String url = request.getRequestURL().toString();
            if (request.getQueryString() != null) {
                url += "?" + request.getQueryString(); // 参数
            }
            modelMap.put("wx_token", accessTokenService.getToken().getToken());
            modelMap.put("wx_ticket", accessTokenService.getTicket().getToken());
            modelMap.put("wx", accessTokenService.sign(url));
            return cms_temppath + "/content_m";
        }
        if (StringUtils.isEmpty(temppath)) {
            temppath = content.getCategory().getAtp();
        }
        if (StringUtils.isEmpty(temppath)) {
            temppath = "content";
        }
        return cms_temppath + "/" + temppath;
    }

    @RequestMapping(value = "/cms/content")
    public String adminPage(ModelMap modelMap) {
        modelMap.put("ws", WebsiteCache.getConfig());
        Map<String, Object> param = ActionUtils._getParameters();
        modelMap.put("param", param);
        modelMap.put("page", contentService.getPage(param));
        return "cms/content_page";
    }

    /**
     * 编辑保存
     * 
     * @param modelMap
     * @param id
     * @return
     */
    @RequestMapping(value = "/cms/content/edit", method = RequestMethod.GET)
    public String edit(ModelMap modelMap, Long id, String type) {
        modelMap.put("ws", WebsiteCache.getConfig());
        Map<String, Object> param = ActionUtils._getParameters();
        modelMap.put("param", param);
        modelMap.put("cglist", categoryService.getSelects());
        modelMap.put("sysDate", UTIL.FORMAT.date(new Date(), "yyyy-MM-dd"));
        if (id != null) {
            Content content = contentService.get(id);
            modelMap.put("data", content);
            type = content.getType();
        }
        if (type == null) {
            type = "content";
        }
        if ("content".equals(type)) {
            return "cms/content_edit";
        } else {
            return "cms/content_edit_" + type;
        }
    }

    /**
     * 文章修改
     * 
     * @param content
     *            文章bean
     * @return {@code org.viva.core.api.Ajax.exec()} {@code @ResponseBody}
     */
    @ResponseBody
    @RequestMapping(value = "/cms/content/save", method = RequestMethod.POST)
    public Map<String, Object> doSave(Content content) {
        content.setUserId(Security.getUser().getId());
        if (content.getId() == null) {
            contentService.add(content);
        } else {
            contentService.up(content);
        }
        tagService.setTags(content.getTags());
        try {
            BaiduSm.art(content.getId() + "");
        } catch (Exception e) {
        }
        return Ajax.exec(content.getId());
    }

    /**
     * 文章删除
     * 
     * @param id
     *            文章ID
     * @return {@code org.viva.core.api.Ajax.exec()} {@code @ResponseBody}
     */
    @ResponseBody
    @RequestMapping(value = "/cms/content/del")
    public Map<String, Object> doDel(Long id) {
        contentService.del(id);
        return Ajax.exec();
    }

    @ResponseBody
    @RequestMapping(value = "/view/a/{id}")
    public String doView(@PathVariable("id") Long id) {
        Content content = contentService.get(id);
        content.setViewCount(content.getViewCount() + 1);
        return "";
    }

}
